'use strict';

angular.module('SystemManage').controller('TempCardController',
    ['$scope', '$stateParams', 'BaseModule', 'BaseFunction', 'BaseEnum', 'BarcodeEnum', '$location', 'Authentication', 'usbPort',
    function($scope, $stateParams, BaseModule, BaseFunction, BaseEnum, BarcodeEnum, $location, Authentication, usbPort) {
        //$scope.authentication = Authentication;
        
        $scope.btnBind = function(){
            if($scope.Module._TempCard.FocusedRecord.TempCardID == undefined || 
                $scope.Module._TempCard.FocusedRecord.TempCardID.trim() == ""){
                BaseFunction.alert("请扫临时卡！", "保存错误");
                return;
            }
            if($scope.Module._TempCard.FocusedRecord.User == undefined || 
                $scope.Module._TempCard.FocusedRecord.User == null){
                BaseFunction.alert("请录入用户！", "保存错误");
                return;
            }

            $scope.Module.BarcodeMaster.FocusedRecord.BarcodeType = BarcodeEnum.BarcodeType.Person.Value;
            $scope.Module.BarcodeMaster.FocusedRecord.Barcode = $scope.Module._TempCard.FocusedRecord.TempCardID;
            $scope.Module.BarcodeMaster.FocusedRecord.EntityID = $scope.Module._TempCard.FocusedRecord.User._id;
            if($scope.Module.BarcodeMaster.FocusedRecord.RowStatus != BaseEnum.RowStatus.Insert.Value){
                $scope.Module.BarcodeMaster.FocusedRecord.RowStatus = BaseEnum.RowStatus.Modify.Value;
            }
            $scope.Module.BarcodeMaster.save();
        };

        $scope.$on('serial', function(event, data) {
            $scope.Module._TempCard.FocusedRecord.TempCardID = hexToNumber(data).toString();
            $scope.$digest();
        });

        usbPort.start();
        $scope.$on('usbPort.input', function(e, inputStr) {
            setCardID(inputStr);
        });
        $scope.$on('$destroy', function() {
            usbPort.stop();
        });

        var setCardID = function(cardID){
            if(cardID != undefined && cardID.length > 0){
                BaseFunction.loading(true);
                //先检查该ID是否存在
                $scope.Module.BarcodeMaster.find({Barcode: cardID}, null, null, function(records){
                    if(records.length > 0){
                        if(records[0].BarcodeType != BarcodeEnum.BarcodeType.Person.Value){
                            BaseFunction.loading(false);
                            BaseFunction.alert("该卡不能作为员工临时卡！", "扫卡错误");                        
                        }
                        else{                            
                            $scope.Module.BarcodeMaster.Records = records;
                            $scope.Module.BarcodeMaster.FocusedRecord = records[0];
                            $scope.Module.User.findById(records[0].EntityID, function(userRecord){
                                $scope.Module._TempCard.FocusedRecord.TempCardID = cardID;
                                $scope.Module._TempCard.FocusedRecord.User = userRecord;
                                BaseFunction.loading(false);
                            });
                        }
                    }
                    else{
                        $scope.Module._TempCard.FocusedRecord.TempCardID = cardID;
                        BaseFunction.loading(false);
                    }
                });
            }
        };

        //初始化
        BaseModule.initModule($scope, '_TempCard');
        $scope.Module._TempCard.PageSize = 1;
        $scope.Module._TempCard.add();

        BaseModule.initModule($scope, 'BarcodeMaster');
        $scope.Module.BarcodeMaster.add();
        BaseModule.initModule($scope, 'User');
    }
]);
